# -*- coding: utf-8 -*-
"""
Created on Fri Nov  9 20:57:38 2012

@author: nico
"""
import numpy as np

from tptsig.wavfile import *
from tptsig.nmf import *
import scipy as sp
import pylab as plt

nb_col=2

filename="/Users/Nicolas/Documents/Projets/TDO/post traitement/tracking freq/s120725d.sig"

with open(str(filename),'rb') as fd:
    temp=np.fromfile(fd,np.int16,-1,"")

y0 = temp[range(256,size(temp),2)]



(Pxx, freqs, bins, im)=plt.specgram(temp, NFFT=4096, noverlap=1024)

# plt.plot(Pxx[:10,:1000].T)

maxim=np.argmax(Pxx,0)
# plt.plot(maxim)

y=Pxx[:,10]
x=freqs

# plt.plot(freqs,log(y))


_max, _min = peakdetect(y)

print _max, _min

xm = [p[0] for p in _max]
ym = [p[1] for p in _max]
xn = [p[0] for p in _min]
yn = [p[1] for p in _min]
    
plt.plot(x,y)
plt.hold(True)
plt.plot(xm, ym, 'r+')
plt.plot(xn, yn, 'g+')
    
# manual spectrogramm
#Nbslices=10000
## slices
#SlicesDim=(Nbslices,size(y0)/Nbslices)
#y0.resize(SlicesDim)
#
#zeropad=2**12
#
#s1=size(np.fft.rfft(y0[1,:],n=zeropad))
#    
#stfft=sp.zeros((s1,Nbslices),dtype=complex,)
#    
#for i in range(Nbslices):
#    stfft[:,i]=np.fft.rfft(y0[i,:],n=zeropad)
#    
#power=log(abs(stfft))**2    
    
#plt.imshow(power[:300,:5000],origin='lower',aspect='auto')